<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'
import fileDownload from 'js-file-download'
import axios from 'axios'

const downloadFile = async () => {
  const file_res = await axios.get('http://localhost:3000/download/file', {
    responseType: 'blob'
  })
  // contentDisposition 中存储的是文件名信息。
  const contentDisposition = file_res.headers['content-disposition']
  // 解决中文乱码问题
  const filename = decodeURI(
    contentDisposition.slice(
      contentDisposition.search('filename=') + 'filename='.length
    )
  )
  fileDownload(file_res.data, filename)
}
</script>

<template>
  <header>
    <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />

    <div class="wrapper">
      <HelloWorld msg="You did it!" />
    </div>
  </header>

  <main>
    <TheWelcome />
    <button class="demo-button" @click="downloadFile">点我下载文件</button>
  </main>
</template>

<style>
@import './assets/base.css';

.demo-button {
  width: 200px;
  height: 200px;
  /* color: aqua; */
  background-color: aquamarine;
}

#app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 2rem;

  font-weight: normal;
}

header {
  line-height: 1.5;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

a,
.green {
  text-decoration: none;
  color: hsla(160, 100%, 37%, 1);
  transition: 0.4s;
}

@media (hover: hover) {
  a:hover {
    background-color: hsla(160, 100%, 37%, 0.2);
  }
}

@media (min-width: 1024px) {
  body {
    display: flex;
    place-items: center;
  }

  #app {
    display: grid;
    grid-template-columns: 1fr 1fr;
    padding: 0 2rem;
  }

  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  .logo {
    margin: 0 2rem 0 0;
  }
}
</style>
